<?php

namespace App\Http\Controllers\Mobile\Company;

use App\Models\Company\CompanyUser;
use Illuminate\Http\Request;
use Style, Auth;


class MpWeiXinController extends CommonController{

    /**
     * 手机微信绑定模版
     * @param Request $request
     * @return mixed
     */
    public function binDing(Request $request){
        $request->only('unionid', 'openid');
        empty($request->unionid) ? $unionid = '' : $unionid = $request->unionid;
        empty($request->openid) ? $openid = '' : $openid = $request->openid;
        empty($request->category) ? $category = '' : $category = $request->category;
        return Style::view('mobile/company/auth/binDing', compact('unionid', 'openid','category'));
    }

    /**
     * 保存手机微信绑定
     * @param Request $request
     */
    public function stroyBinDing(Request $request){

        $input = $request->only('unionid', 'openid', 'username', 'password', 'remember');
        if(!empty($request->openid)){
            empty($input['unionid']) ? $input['unionid'] = NULL : '';
            //用户和手机登录多可以
            $remember = !empty($input['remember']) ? $input['remember'] : false;
            if(Auth::guard('company')->attempt(['username' => $input['username'], 'password' => $input['password']], $remember)){
                //普通绑定
                $user = Auth::guard('company')->user();

                if(CompanyUser::where('uid', $user->uid)->update(['wechatmpopenid' => $input['openid'], 'wechatunionid' => $input['unionid']])){
                    $status = "success";
                }else{
                    $status = "flase";
                }
                return Style::view('mobile/company/auth/binDingStatus', compact('status'));
            }else{
                $link = "?unionid=" . $request->unionid . "&openid=" . $request->openid;
                return redirect(url("mobile/company/binding" . $link))->with(['status' => '账号或密码错误']);
                //return Style::view('mobile/company/auth/binDingStatus',compact('status'));
            }
        }

    }

    /**
     * 注册绑定
     * @param Request $request
     * @return mixed
     */
    public function regBinDing(Request $request){
        $request->only('unionid', 'openid', 'category');
        Auth::guard('company')->logout();//清除上次登录
        session()->put('unionid', $request->unionid);
        session()->put('openid', $request->openid);
        session()->put('category', $request->category);
        return Style::view('mobile/company/auth/registerBinding');
    }

    /**
     * 绑定状态，图片提醒
     * @param Request $request
     * @return mixed
     */
    public function binDingStatus(){
        $status = "success";
        return Style::view('mobile/company/auth/binDingStatus', compact('status'));
    }
}